// controller 层 用于处理路由中的函数

const { doRegister, generateCaptcha, doLogin } = require("../service/auth");

module.exports = {
  async register(ctx) {
    ctx.verifyParams({
      username: { type: "string", required: true },
      nickname: { type: "string", required: true },
      password: { type: "string", required: true },
    });

    await doRegister(ctx, ctx.request.body);

    ctx.body = {
      code: 200,
      message: "注册成功",
      data: true,
    };
  },

  async captcha(ctx) {
    const result = await generateCaptcha(ctx);
    ctx.body = {
      code: 0,
      message: "验证码获取成功",
      data: result,
    };
  },

  async login(ctx) {
    ctx.verifyParams({
      username: { type: "string", required: true },
      password: { type: "string", required: true },
      captchaKey: { type: "string", required: true },
      captchaCode: { type: "string", required: true },
    });

    const token = await doLogin(ctx, ctx.request.body);

    ctx.body = {
      code: 200,
      message: "登录成功",
      data: token,
    };
  },
};
